package fatecpg.com.br.japassei;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import fatecpg.com.br.japassei.bd.Disciplina;
import fatecpg.com.br.japassei.bd.DisciplinaDAO;


public class DisciplinaActivity extends Activity {
    Disciplina disciplina;
    Boolean editar = null;
    Boolean novo = null;
    Boolean ver = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_disciplina);

       Intent editar = getIntent();
       if (editar.hasExtra("ic_editar") || editar.hasExtra("ic_ver")) {
           if (editar.getBooleanExtra("ic_editar", false) || editar.getBooleanExtra("ic_ver", false)) {

               int cod = Integer.parseInt(editar.getStringExtra("cd_disciplina"));
               String nome = editar.getStringExtra("nm_disciplina");
               String desc = editar.getStringExtra("ds_disciplina");
               this.disciplina = new Disciplina(cod, nome, desc);

               getNmDisciplina().setText(disciplina.getNmDisciplina());
               getDsDisciplina().setText(disciplina.getDsDisciplina());
           }
           this.editar = editar.getBooleanExtra("ic_editar", false);
           this.novo = editar.getBooleanExtra("ic_novo", false);
           this.ver =editar.getBooleanExtra("ic_ver", false);
       }

        manipulabotoes();
    }

    public void salvar(View view) {
        DisciplinaDAO disciplinaDAO = new DisciplinaDAO(this.getApplicationContext());
        if(this.editar) {
            if (disciplinaDAO.upDisciplina(this.disciplina.getCdDisciplina(), getNmDisciplina().getText().toString(), getDsDisciplina().getText().toString())) {
                Toast.makeText(getApplicationContext(), "Atualizado", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "Erro ao atualizar", Toast.LENGTH_SHORT).show();
            }
        }else if(this.novo){
            disciplina = new Disciplina(getNmDisciplina().getText().toString(), getDsDisciplina().getText().toString());
            if (disciplinaDAO.setDisciplina(disciplina)) {
                Toast.makeText(getApplicationContext(), "Atualizado", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "Erro ao atualizar", Toast.LENGTH_SHORT).show();
            }
        }
        finish();
    }

    public void cancelar(View view) {
        finish();
    }

    public void editar(View view){
        this.editar = true;
        manipulabotoes();
    }

    public void manipulabotoes(){
        ImageButton imbSalvar = (ImageButton) findViewById(R.id.imbSalvar);
     //   ImageButton imbEditar = (ImageButton) findViewById(R.id.imbEditar);
        if (this.ver) {
            getNmDisciplina().setEnabled(false);
            getDsDisciplina().setEnabled(false);
            imbSalvar.setEnabled(false);
    //        imbEditar.setEnabled(true);
            imbSalvar.setImageResource(R.drawable.ic_salvar_cinza);
    //        imbEditar.setImageResource(R.drawable.ic_editar2);

           getNmDisciplina().setText(disciplina.getNmDisciplina());
           getDsDisciplina().setText(disciplina.getDsDisciplina());
        }else if(this.editar){
            getNmDisciplina().setEnabled(true);
            getDsDisciplina().setEnabled(true);
            imbSalvar.setEnabled(true);
            imbSalvar.setImageResource(R.drawable.ic_salvar);
      //      imbEditar.setImageResource(R.drawable.ic_editar_cinza);
        }else if(this.novo){
      //      imbEditar.setImageResource(R.drawable.ic_editar_cinza);
        }
    }
    public EditText getNmDisciplina(){
        return (EditText) findViewById(R.id.txtNmDisciplina);
    }

    public EditText getDsDisciplina(){
        return (EditText) findViewById(R.id.txtDsDisciplina);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_disciplina, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
